# -*- coding: iso-8859-2 -*-

from authorshipanalyzer.slot.filler import SexFiller
from authorshipanalyzer.slot.filler import DiacriticsFiller
from authorshipanalyzer.slot.filler import CategoryFiller 
from authorshipanalyzer.slot.filler import StructureFeaturesFiller
from authorshipanalyzer.slot.filler import ProfileFiller
from authorshipanalyzer.slot.filler import WordFeaturesFiller
from authorshipanalyzer.slot.filler import DocumentFeaturesFiller
from authorshipanalyzer.slot import Slot


class SlotFiller:
    '''
    Class filling the slot of a message
    '''
    def __init__(self):
        self.fillers = []      
        self.addFiller(SexFiller())
        self.addFiller(DiacriticsFiller())
        self.addFiller(StructureFeaturesFiller())
        self.addFiller(WordFeaturesFiller())
        self.addFiller(DocumentFeaturesFiller())
        self.addFiller(ProfileFiller())
        self.addFiller(CategoryFiller())

        
    def addFiller(self, filler):
        self.fillers.append(filler)
       
    def createSlot(self, message):
        '''
        Create and fill a slot
        @param message: message for slot filling
        @return slot filled using a single message
        '''
        slot = Slot()
        slot.message = message
        
        for filler in self.fillers:
            filler.fill(slot)
        
        return slot 
        
if __name__ == "__main__":
    import datetime
    import os
    
    from authorshipanalyzer.message import MessageReader
    from authorshipanalyzer.file import Path
    from time import time
    
    fromDate = datetime.date(2008, 2, 1)
    toDate = datetime.date(2008, 2, 3)
    
    start = time()
    mr = MessageReader(Path.getInfo1Dir())
    messages = mr.readMessages(fromDate, toDate)
    end = time()
    print "Number of parsed messages: " + str(len(messages))
    print "Parse time: " + str(end - start) + " seconds"
    
    sf = SlotFiller()
    
#    # test
#    i = 0
#    f = open('/tmp/parsetest.txt', 'a')
#    for message in messages:
#        slot = sf.createSlot(message)
#        
#        f.write(str(i) + "\n")
#        f.write("age: " + str(slot.age) + "\n")
#        f.write("status: " + str(slot.status) + "\n")
#        
#        text = slot.greeting
#        for line in text.split("\n"):
#            f.write("gret: " + line + "\n")
#          
#        text = slot.text
#        for line in text.split("\n"):
#            f.write("txt: " + line + "\n")
#            
#        text = slot.farewell
#        for line in text.split("\n"):
#            f.write("far: " + line + "\n")
#            
#        text = slot.signature
#        for line in text.split("\n"):
#            f.write("sig: " + line + "\n")  
#            
#        f.write("\n\n\n\n")
#        print i
#        i += 1
#    f.close()
#    
    for message in messages:
        print message.getSubject()
        print message.getAuthor()
        print message.getText()[:100]
        
        slot = sf.createSlot(message)
        
        print "Author:\t" + slot.author
        
        print "Sex:\t" + slot.sex
        print "Age:\t" + str(slot.age)
        print "Status:\t" + str(slot.status)
        
        print "Category:\t" + str(slot.category)
 
        print "Has advert:\t" + str(slot.hasAdvert)
        print "Diacritics:\t" + str(slot.useDiacritics)
        print "Requotation type:\t" + str(slot.requotationType)
        
#        print "\nCharacter based features:"
#        print "Alphabetic chars:\t" + str(slot.cAlphabetic)
#        print "Upper case chars:\t" + str(slot.cUpperCase)
#        print "Digit chars:\t" + str(slot.cDigit)
#        print "White space chars:\t" + str(slot.cWhiteSpace)
#        print "Space chars:\t" + str(slot.cSpace)
#        print "Space per white char:\t" + str(slot.cSpacePerWhite)
#        print "Tab chars:\t" + str(slot.cTab)
#        print "Tab per white space char:\t" + str(slot.cTabPerWhite)
#        print "Punctuation chars:\t" + str(slot.cPunctuation)
        
#        print "\nWord based features:"
#        print "Average word length:\t" + str(slot.wAvgWordLength)
#        print "Vocabulary richness:\t" + str(slot.wVocabularyRichness)
##        print slot.wFunctionWord
#        print "Short words: " + str(slot.wShortWords)
#        print "Hapax Legomena / N:\t" + str(slot.wHapaxLegomentaN)
#        print "Hapax Legomena / V:\t" + str(slot.wHapaxLegomentaV)
#        for pos, proc in slot.wPos.items():
#            print "Part-of-Speech:\t" + pos + " " + str(proc)
        
        print "\nDocument based features:"
#        print "Blank lines:\t" + str(slot.dBlankLines)
        print "Sentences:\t" + str(slot.dSentences)
        
        print "\nMessage parts:"
        for line in slot.greeting.split("\n"):
            print "<grt> " + line
        for line in slot.text.split("\n"):
            print "<txt> " + line
        for line in slot.farewell.split("\n"):
            print "<far> " + line
        for line in slot.signature.split("\n"):
            print "<sig> " + line
            
#        print "Greeting:\n" + slot.greeting
#        print "Text:\n" + slot.text
#        print "Farewell:\n" + slot.farewell
#        print "Signature:\n" + slot.signature

#        print "Orginal message:\n"
#        print slot.message.getText()
        print "\n\n\n\n++++++++++--------------+++++++++++\n\n\n\n"
        